﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using COM.Rights.BLL;
using COM.Rights.Model;
using System.Data;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["Action"] == "Logout")
            {
                ZASession.MemberName = null;
                ZRSession.Functions = null;
                
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string loginIP = ZRequest.IP;
        if ( txtCode == null || string.IsNullOrEmpty(txtCode.Text) ||  txtCode.Text.ToUpper() != ZASession.ValidateCodeAdmin.ToUpper())
        {
            //ZJScript.Alert("验证码不正确！");
            ClientScript.RegisterStartupScript(GetType(), "Error", "<script>alert('验证码不正确');</script>");
            ZASession.ValidateCodeAdmin = null;
            return;
        }

        /*不验证IP*/
        //TUser tUser = new TUser();
        //string sql = "select IPs from TUserRightsIp where UID='" + txtUser.Text + "'";
        //DataTable dt = new DataTable();
        //dt.Load(tUser.ExecuteReader(sql));
        //if (dt.Rows.Count > 0)
        //{
        //    string TUserRightsIp = dt.Rows[0][0].ToString();
        //    if (TUserRightsIp.Contains(loginIP + ",") || Request.UserHostAddress == "127.0.0.1" || Request.UserHostAddress == "::1")
        //    {
        TUser _TUser = new TUser();
        TUserInfo _TUserInfo = new TUserInfo();

        List<DBParameter> paras = new List<DBParameter>();
        paras.Add(new DBParameter("UID", txtUser.Text));
        paras.Add(new DBParameter("PWD", txtPassowrd.Text));

        _TUserInfo = _TUser.GetModel("", "UID=@UID AND PWD=@PWD AND IsEnable=1 ", "", paras);

        if (_TUserInfo != null)
        {
            Dictionary<string, TFunctionInfo> userInfo = new Dictionary<string, TFunctionInfo>();
            TFunction _TFunction = new TFunction();
            userInfo = _TFunction.GetFunctionByUser(_TUserInfo.GID);
            ZRSession.RoleNames = _TFunction.GetRoleNameList();
            ZASession.MemberName = _TUserInfo.Name;
            ZRSession.MemberUID = _TUserInfo.GID;
            ZASession.Account = _TUserInfo.UID;
            ZRSession.Functions = userInfo;
            Thread.Sleep(ZSetup.SleepTimes);
            //保存用户名，以备地图中调用
            Response.Cookies["uid"].Value = ZASession.Account;

            Response.Redirect("Default.aspx");
        }
        else
        {
            ZJScript.Alert("用户名或者密码错误！");
        }
        //    }
        //    else
        //    {
        //        ZJScript.Alert("登录未授权!" + loginIP);
        //    }
        //}
        //else
        //{
        //    ZJScript.Alert("登录未授权!" + loginIP);
        //}

    }
}